<html lang="zh-CN"><head><meta charset="UTF-8"><style>.nodata  main {width:1000px;margin: auto;}</style></head><body class="nodata " style=""><div class="main_father clearfix d-flex justify-content-center " style="height:100%;"> <div class="container clearfix " id="mainBox"><main><div class="blog-content-box">
<div class="article-header-box">
<div class="article-header">
<div class="article-title-box">
<h1 class="title-article" id="articleContentId">(C卷,200分)- 转骰子（Java & JS & Python）</h1>
</div>
</div>
</div>
<div id="blogHuaweiyunAdvert"></div>

        <div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-044f2cf1dc.css">
                <div id="content_views" class="htmledit_views">
                    <h4 id="main-toc">题目描述</h4> 
<p>骰子是一个立方体&#xff0c;每个面一个数字&#xff0c;初始为左1&#xff0c;右2&#xff0c;前3(观察者方向)&#xff0c;后4&#xff0c;上5&#xff0c;下6&#xff0c;用123456表示这个状态&#xff0c;放置在平面上&#xff0c;</p> 
<ul><li>可以向左翻转(用L表示向左翻转1次)&#xff0c;</li><li>可以向右翻转(用R表示向右翻转1次)&#xff0c;</li><li>可以向前翻转(用F表示向前翻转1次)&#xff0c;</li><li>可以向后翻转(用B表示向后翻转1次)&#xff0c;</li><li>可以逆时针旋转(用A表示逆时针旋转90度)&#xff0c;</li><li>可以顺时针旋转(用C表示顺时针旋转90度)&#xff0c;</li></ul> 
<p>现从123456这个初始状态开始&#xff0c;根据输入的动作序列&#xff0c;计算得到最终的状态。</p> 
<p>骰子的初始状态和初始状态转动后的状态如图所示。</p> 
<p style="text-align:center;"><img alt="" src="https://img-blog.csdnimg.cn/5c20c4bcdf0d475a9e5aaf4a519de48c.png" /></p> 
<p></p> 
<h4 id="%E8%BE%93%E5%85%A5%E6%8F%8F%E8%BF%B0">输入描述</h4> 
<p>输入一行&#xff0c;为只包含LRFBAC的字母序列&#xff0c;最大长度为50&#xff0c;字母可重复。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%87%BA%E6%8F%8F%E8%BF%B0">输出描述</h4> 
<p>输出最终状态</p> 
<p></p> 
<h4 id="%E7%94%A8%E4%BE%8B">用例</h4> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">L R</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">123456</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">无</td></tr></tbody></table> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">F C R</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">342156</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">无</td></tr></tbody></table> 
<h4 id="%E9%A2%98%E7%9B%AE%E8%A7%A3%E6%9E%90">题目解析</h4> 
<p>本题感觉就是一道耗时题&#xff0c;考察细心程度的。具体的逻辑反而不是很难。</p> 
<p>具体逻辑请看源码。</p> 
<p></p> 
<h4 id="%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81">JavaScript算法源码</h4> 
<pre><code class="language-javascript">/* JavaScript Node ACM模式 控制台输入获取 */
const readline &#61; require(&#34;readline&#34;);
 
const rl &#61; readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});
 
rl.on(&#34;line&#34;, (line) &#61;&gt; {
  const directives &#61; line.split(&#34; &#34;);
  turnDice(directives);
});
 
function turnDice(directives) {
  const dice &#61; new Dice();
 
  directives.forEach((directive) &#61;&gt; {
    switch (directive) {
      case &#34;L&#34;:
        dice.turnL();
        break;
      case &#34;R&#34;:
        dice.turnR();
        break;
      case &#34;F&#34;:
        dice.turnF();
        break;
      case &#34;B&#34;:
        dice.turnB();
        break;
      case &#34;A&#34;:
        dice.turnA();
        break;
      case &#34;C&#34;:
        dice.turnC();
        break;
    }
  });
 
  dice.print();
}
 
class Dice {
  constructor() {
    this.left &#61; 1;
    this.right &#61; 2;
    this.front &#61; 3;
    this.back &#61; 4;
    this.top &#61; 5;
    this.bottom &#61; 6;
  }
 
  turnL() {
    // 前后不变&#xff0c;上变左&#xff0c;左变下&#xff0c;下变右&#xff0c;右变上
    let tmp &#61; this.right;
    this.right &#61; this.bottom;
    this.bottom &#61; this.left;
    this.left &#61; this.top;
    this.top &#61; tmp;
  }
 
  turnR() {
    // 前后不变&#xff0c;上变右&#xff0c;右变下&#xff0c;下变左&#xff0c;左变上
    let tmp &#61; this.left;
    this.left &#61; this.bottom;
    this.bottom &#61; this.right;
    this.right &#61; this.top;
    this.top &#61; tmp;
  }
 
  turnF() {
    // 左右不变&#xff0c;上变前&#xff0c;前变下&#xff0c;下变后&#xff0c;后变上
    let tmp &#61; this.front;
    this.front &#61; this.top;
    this.top &#61; this.back;
    this.back &#61; this.bottom;
    this.bottom &#61; tmp;
  }
 
  turnB() {
    // 左右不变&#xff0c;前变上&#xff0c;上变后&#xff0c;后变下&#xff0c;下边前
    let tmp &#61; this.top;
    this.top &#61; this.front;
    this.front &#61; this.bottom;
    this.bottom &#61; this.back;
    this.back &#61; tmp;
  }
 
  turnA() {
    // 上下不变&#xff0c; 前变右&#xff0c;右变后&#xff0c;后变左&#xff0c;左变前
    let tmp &#61; this.right;
    this.right &#61; this.front;
    this.front &#61; this.left;
    this.left &#61; this.back;
    this.back &#61; tmp;
  }
 
  turnC() {
    // 上下不变&#xff0c; 右变前&#xff0c;前变左&#xff0c;左变后&#xff0c;后变右
    let tmp &#61; this.front;
    this.front &#61; this.right;
    this.right &#61; this.back;
    this.back &#61; this.left;
    this.left &#61; tmp;
  }
 
  print() {
    let { left, right, front, back, top, bottom } &#61; this;
    console.log(&#96;${left}${right}${front}${back}${top}${bottom}&#96;);
  }
}</code></pre> 
<p></p> 
<h4>Java算法源码</h4> 
<pre><code class="language-java">import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc &#61; new Scanner(System.in);
    String[] directives &#61; sc.nextLine().split(&#34; &#34;);
    turnDice(directives);
  }

  public static void turnDice(String[] directives) {
    Dice dice &#61; new Dice();

    for (String directive : directives) {
      switch (directive) {
        case &#34;L&#34;:
          dice.turnL();
          break;
        case &#34;R&#34;:
          dice.turnR();
          break;
        case &#34;F&#34;:
          dice.turnF();
          break;
        case &#34;B&#34;:
          dice.turnB();
          break;
        case &#34;A&#34;:
          dice.turnA();
          break;
        case &#34;C&#34;:
          dice.turnC();
          break;
      }
    }

    dice.print();
  }
}

class Dice {
  int left &#61; 1;
  int right &#61; 2;
  int front &#61; 3;
  int back &#61; 4;
  int top &#61; 5;
  int bottom &#61; 6;

  public void turnL() {
    // 前后不变&#xff0c;上变左&#xff0c;左变下&#xff0c;下变右&#xff0c;右变上
    int tmp &#61; this.right;
    this.right &#61; this.bottom;
    this.bottom &#61; this.left;
    this.left &#61; this.top;
    this.top &#61; tmp;
  }

  public void turnR() {
    // 前后不变&#xff0c;上变右&#xff0c;右变下&#xff0c;下变左&#xff0c;左变上
    int tmp &#61; this.left;
    this.left &#61; this.bottom;
    this.bottom &#61; this.right;
    this.right &#61; this.top;
    this.top &#61; tmp;
  }

  public void turnF() {
    // 左右不变&#xff0c;上变前&#xff0c;前变下&#xff0c;下变后&#xff0c;后变上
    int tmp &#61; this.front;
    this.front &#61; this.top;
    this.top &#61; this.back;
    this.back &#61; this.bottom;
    this.bottom &#61; tmp;
  }

  public void turnB() {
    // 左右不变&#xff0c;前变上&#xff0c;上变后&#xff0c;后变下&#xff0c;下边前
    int tmp &#61; this.top;
    this.top &#61; this.front;
    this.front &#61; this.bottom;
    this.bottom &#61; this.back;
    this.back &#61; tmp;
  }

  public void turnA() {
    // 上下不变&#xff0c; 前变右&#xff0c;右变后&#xff0c;后变左&#xff0c;左变前
    int tmp &#61; this.right;
    this.right &#61; this.front;
    this.front &#61; this.left;
    this.left &#61; this.back;
    this.back &#61; tmp;
  }

  public void turnC() {
    // 上下不变&#xff0c; 右变前&#xff0c;前变左&#xff0c;左变后&#xff0c;后变右
    int tmp &#61; this.front;
    this.front &#61; this.right;
    this.right &#61; this.back;
    this.back &#61; this.left;
    this.left &#61; tmp;
  }

  public void print() {
    String sb &#61;
        String.valueOf(this.left) &#43; this.right &#43; this.front &#43; this.back &#43; this.top &#43; this.bottom;
    System.out.println(sb);
  }
}
</code></pre> 
<p></p> 
<h4>Python算法源码</h4> 
<pre><code class="language-python"># 算法入口
def turnDice(directives):
    dice &#61; Dice()

    for directive in directives:
        if directive &#61;&#61; &#34;L&#34;:
            dice.turnL()
        elif directive &#61;&#61; &#34;R&#34;:
            dice.turnR()
        elif directive &#61;&#61; &#34;F&#34;:
            dice.turnF()
        elif directive &#61;&#61; &#34;B&#34;:
            dice.turnB()
        elif directive &#61;&#61; &#34;A&#34;:
            dice.turnA()
        elif directive &#61;&#61; &#34;C&#34;:
            dice.turnC()

    return str(dice)

# 筛子实现类
class Dice:
    def __init__(self):
        self.left &#61; 1
        self.right &#61; 2
        self.front &#61; 3
        self.back &#61; 4
        self.top &#61; 5
        self.bottom &#61; 6

    def turnL(self):
        # 前后不变&#xff0c;上变左&#xff0c;左变下&#xff0c;下变右&#xff0c;右变上
        self.right, self.bottom, self.left, self.top &#61; self.bottom, self.left, self.top, self.right

    def turnR(self):
        # 前后不变&#xff0c;上变右&#xff0c;右变下&#xff0c;下变左&#xff0c;左变上
        self.left, self.bottom, self.right, self.top &#61; self.bottom, self.right, self.top, self.left

    def turnF(self):
        # 左右不变&#xff0c;上变前&#xff0c;前变下&#xff0c;下变后&#xff0c;后变上
        self.front, self.top, self.back, self.bottom &#61; self.top, self.back, self.bottom, self.front

    def turnB(self):
        # 左右不变&#xff0c;前变上&#xff0c;上变后&#xff0c;后变下&#xff0c;下边前
        self.top, self.front, self.bottom, self.back &#61; self.front, self.bottom, self.back, self.top

    def turnA(self):
        # 上下不变&#xff0c; 前变右&#xff0c;右变后&#xff0c;后变左&#xff0c;左变前
        self.right, self.front, self.left, self.back &#61; self.front, self.left, self.back, self.right

    def turnC(self):
        # 上下不变&#xff0c; 右变前&#xff0c;前变左&#xff0c;左变后&#xff0c;后变右
        self.front, self.right, self.back, self.left &#61; self.right, self.back, self.left, self.front

    def __str__(self):
        return f&#34;{self.left}{self.right}{self.front}{self.back}{self.top}{self.bottom}&#34;


# 输入获取
directives &#61; input().split()

# 算法调用
print(turnDice(directives))
</code></pre>
                </div>
        </div>
        <div id="treeSkill"></div>
        <div id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"></div>
    <script>
  $(function() {
    setTimeout(function () {
      var mathcodeList = document.querySelectorAll('.htmledit_views img.mathcode');
      if (mathcodeList.length > 0) {
        for (let i = 0; i < mathcodeList.length; i++) {
          if (mathcodeList[i].naturalWidth === 0 || mathcodeList[i].naturalHeight === 0) {
            var alt = mathcodeList[i].alt;
            alt = '\\(' + alt + '\\)';
            var curSpan = $('<span class="img-codecogs"></span>');
            curSpan.text(alt);
            $(mathcodeList[i]).before(curSpan);
            $(mathcodeList[i]).remove();
          }
        }
        MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
      }
    }, 1000)
  });
</script>
</div></html>